package com.delio.client.pagos.empleados;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.util.Utilities;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;

public class PanelNuevoEmpleado extends FormPanel {
	private DelioConstants k = GWT.create(DelioConstants.class);
	private FormBinding formbinding;
	private HiddenField<String> id;
	private TextField<String> txtDni;
	private TextField<String> txtNombres;
	private TextField<String> txtApellidos;
	private TextField<String> txtDomicilio;
	private TextField<String> txtTelefono;
	private TextField<String> txtCelular;
	private TextField<String> txtCorreo;

	public PanelNuevoEmpleado() {
		super();
		setScrollMode(Scroll.AUTOY);
		setHeaderVisible(false);
		setLabelAlign(LabelAlign.LEFT);
		setPadding(3);
		setBodyBorder(false);
		
		PanelGridEmpleado.panelnuevoempleado=this;
		formbinding=new FormBinding(this);
		
		
		
		id = new HiddenField<String>();
		
		
		txtDni = new TextField<String>();
		txtDni.setFieldLabel(k.dni());
		txtDni.setMinLength(8);
		txtDni.focus();
		txtDni.setAllowBlank(false);

		txtNombres = new TextField<String>();
		txtNombres.setFieldLabel(k.Nombres());
		txtNombres.setAllowBlank(false);
		Utilities.setNumerosEnteros(txtDni);
		Utilities.setLengh(txtDni, txtNombres, 8);

		txtApellidos = new TextField<String>();
		txtApellidos.setFieldLabel(k.Apellidos());
		txtApellidos.setAllowBlank(false);

		txtDomicilio = new TextField<String>();
		txtDomicilio.setFieldLabel(k.Domicilio());
		txtDomicilio.setAllowBlank(false);

		txtTelefono = new TextField<String>();
		txtTelefono.setMinLength(7);
		txtTelefono.setFieldLabel(k.Telefono());
		

		txtCelular = new TextField<String>();
		txtCelular.setFieldLabel(k.Celular());
		txtCelular.setMinLength(7);
		Utilities.setNumerosEnteros(txtTelefono);
		Utilities.setLengh(txtTelefono, txtCelular, 12);

		txtCorreo = new TextField<String>();
		txtCorreo.setFieldLabel(k.Correo());
		Utilities.validarEmail(txtCorreo);

		Utilities.setNumerosEnteros(txtCelular);
		Utilities.setLengh(txtCelular, txtCorreo, 9);
		
		add(id);
		add(txtDni, new FormData("70%"));
		add(txtNombres, new FormData("70%"));
		add(txtApellidos, new FormData("95%"));
		add(txtDomicilio, new FormData("95%"));
		add(txtTelefono, new FormData("70%"));
		add(txtCelular, new FormData("70%"));
		add(txtCorreo, new FormData("95%"));
		
		setStore(Empleados.store);
	}
	
	
	public void setStore(ListStore<BeanModel> store) {

		formbinding.addFieldBinding(new FieldBinding(id, "id"));
		formbinding.addFieldBinding(new FieldBinding(txtDni, "dni"));
		formbinding.addFieldBinding(new FieldBinding(txtNombres, "nombres"));
		formbinding.addFieldBinding(new FieldBinding(txtApellidos, "apellidos"));
		formbinding.addFieldBinding(new FieldBinding(txtDomicilio, "domicilio"));
		formbinding.addFieldBinding(new FieldBinding(txtTelefono, "telefono"));
		formbinding	.addFieldBinding(new FieldBinding(txtCelular, "celular"));
		formbinding.addFieldBinding(new FieldBinding(txtCorreo, "correo"));
		formbinding.setStore(store);
		
	}

	@Override
	public void reset() {
		id.clear();
		txtDni.clear();
		txtNombres.clear();
		txtApellidos.clear();
		txtDomicilio.clear();
		txtTelefono.clear();
		txtCelular.clear();
		txtCorreo.clear();
		
		PanelGridEmpleado.grid.getSelectionModel().deselectAll();
		unbind();

	}

	public void bind(BeanModel selectedModel) {
		formbinding.bind(selectedModel);
		setHeading(k.ModificarEmpleado());

	}

	public void unbind() {
		formbinding.unbind();
		setHeading(k.NuevoEmpleado());
	}
	
	public void save(){
		Integer empleado_id;
		
			
		String dni=txtDni.getValue();
		String nombres=txtNombres.getValue();
		String apellidos=txtApellidos.getValue();
		String domicilio=txtDomicilio.getValue();
		String telefono=txtTelefono.getValue();
		String celular=txtCelular.getValue();
		String correo=txtCorreo.getValue();
		
		Empleado emp=new Empleado();
		
	
		
		if(id.getValue()!=null){
			empleado_id=Integer.valueOf(id.getValue().toString());
		
			emp.setId(empleado_id);
			}
		
		emp.setDni(dni);
		emp.setNombres(nombres);
		emp.setApellidos(apellidos);
		emp.setDomicilio(domicilio);
		emp.setTelefono(telefono);
		emp.setCelular(celular);
		emp.setCorreo(correo);
		
		Empleados.save(emp);
		Empleados.panelnuevoempleado=PanelNuevoEmpleado.this;
		
	}
}
